package com.woniuxy.ui;

import com.woniuxy.entity.Student;
import com.woniuxy.service.StudentService;
import com.woniuxy.service.impl.StudentServiceImpl;

import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-09-17 10:30
 */
public class StartMenu {

    static Scanner scanner = new Scanner(System.in);

    static StudentService studentService=new StudentServiceImpl();

    public static void show(){
        boolean flag = true;
        while (flag) {
            System.out.println("欢迎使用xxx系统");
            System.out.println("1、登录");
            System.out.println("2、注册");
            System.out.println("3、退出");
            int num = scanner.nextInt();
            switch (num){
                case 1:
                    System.out.println("执行登录");
                    login();
                    break;
                case 2:
                    System.out.println("执行注册");
                    regist();
                    break;
                case 3:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    private static void login() {
        System.out.println("输入用户名");
        String uname = scanner.next();
        System.out.println("输入密码");
        String password = scanner.next();
        try {
            Student student = studentService.login(uname, password);
            System.out.println("登录成功,进入主菜单");
            MainMenu.show();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }

    private static void regist() {
        Student student = new Student();
        System.out.println("输入学号");
        student.setSid(scanner.nextInt());
//        String name=null;
        //用户名3-5位之间，输入错误，一直输入，知道格式正确才能输入下一个数据
        System.out.println("输入用户名");
        while (true) {
            student.setSname(scanner.next());
            if(student.getSname().length()>=3 && student.getSname().length()<=5){
                break;
            }else{
                System.out.println("用户名长度3-5之间，重写输入：");
            }
        }
        System.out.println("输入密码");
        student.setPassword(scanner.next());
        System.out.println("输入年龄");
        student.setAge(scanner.nextInt());
        System.out.println("输入邮箱");
        while (true) {
            student.setEmail(scanner.next());
            if(student.getEmail().matches("^\\w+@\\w+\\.\\w+$")){
                break;
            }else{
                System.out.println("邮箱格式错，重新输入:");
            }
        }
        System.out.println("输入手机号码");
        student.setPhone(scanner.next());
        //表示层调用业务逻辑层
        try {
            studentService.regist(student);
            System.out.println("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
}
